package com.meepo.feed.action

import android.widget.ImageView
import android.widget.TextView
import com.meepo.basic.router.RouterManager
import com.meepo.basic.router.RouterManager.FeedModule.ActionListFragment_Path
import com.meepo.basic.components.listfragment.MeepoListFragment
import com.meepo.framework.design.recycler.ViewHolder
import com.meepo.feed.R
import com.meepo.feed.API
import com.meepo.basic.schema.Enums
import com.meepo.basic.schema.common.PageResult
import com.meepo.basic.schema.common.ResponseWrapper
import com.meepo.basic.schema.feed.Action
import com.xiaojinzi.component.anno.FragmentAnno
import com.xiaojinzi.component.support.ParameterSupport
import io.reactivex.Observable

@FragmentAnno(ActionListFragment_Path)
class ActionListFragment : MeepoListFragment<Action>() {

    override fun loadDataObservable(page: Int): Observable<ResponseWrapper<PageResult<Action>>> {
        val itemType = ParameterSupport.getString(requireArguments(), "itemType") ?: ""
        val actionType = ParameterSupport.getString(requireArguments(), "actionType") ?: ""
        val authorUserId = ParameterSupport.getString(requireArguments(), "authorUserId") ?: ""
        val targetUserId = ParameterSupport.getString(requireArguments(), "targetUserId") ?: ""
        return API.instance().fetchActions(itemType, actionType, authorUserId, targetUserId, page)
    }

    override fun bind(item: Action, holder: ViewHolder, position: Int) {
        holder.itemView.setOnClickListener {

            if (item.targetType == Enums.ITEM_TYPE_ITEM) {
                RouterManager.FeedModule.routeToFeedComment(requireContext(), item.targetId)
            } else if (item.targetType == Enums.ITEM_TYPE_COMMENT) {
                RouterManager.FeedModule.routeToFeedCommentReply(requireContext(), item.targetId)
            }
        }

        holder.setImagePath(R.id.avatar_ci, item.user?.avatar ?: "")
        holder.getView<ImageView>(R.id.avatar_ci)?.setOnClickListener {
            RouterManager.UserModule.routeToUserProfile(requireContext(), item.user?.id)
        }

        holder.setText(R.id.nickname_tv, item.user?.nickname ?: "")
        holder.getView<TextView>(R.id.nickname_tv)?.setOnClickListener {
            RouterManager.UserModule.routeToUserProfile(requireContext(), item.user?.id)
        }

        holder.setText(R.id.detail_tv, item.attrs?.detail ?: "")
        holder.setImagePath(R.id.detail_ci, item.attrs?.icon ?: "")

        holder.setText(R.id.target_tv, item.attrs?.title)

    }

    override fun getItemViewLayoutId(item: Action, position: Int): Int = R.layout.action_list_item_layout
    override fun getItemSkeletonViewLayoutId(): Int {
        return R.layout.action_list_item_skeleton_layout
    }
}